package map;

import java.util.*;

/**
 * 819. 最常见的单词
 */
public class MostCommonWord {
    public static String mostCommonWord(String paragraph, String[] banned) {
        paragraph = paragraph.replaceAll("[!?',;.]", " ");
        String[] paragraphArr = paragraph.toLowerCase().split(" ");
        Set<String> set = new HashSet<>();
        Collections.addAll(set, banned);
        Map<String, Integer> map = new HashMap<>();
        int max = 0;
        String maxString = "";
        for (String str : paragraphArr) {
            if (!set.contains(str) && !str.equals("")) {
                map.put(str, map.getOrDefault(str, 0) + 1);
                int count = map.get(str);
                if (count > max) {
                    max = count;
                    maxString = str;
                }
            }
        }
        return maxString;
    }

    public static void main(String[] args) {
        String paragraph = "Bob. hIt, baLl";
        String[] banned = new String[] {"bob", "hit"};
        System.out.println(mostCommonWord(paragraph, banned));
    }
}
